/*
Content: Generates monthly-country data from the GTD original database + relevant and isolated events
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/


*****************************************
** COUNTRY CODES MATCH IEB AND GTD ******
*****************************************

global terror "globalterror"
global armedconflict "uppsala"

preserve
	foreach type in terror armedconflict {
	import excel "$codes\countrycodes_ieb2.xlsx", clear sheet("`type'") firstrow
	save "${`type'}/help/countrycodes_ieb_`type'.dta", replace
	}
	
restore



***************************************************************************
** UPPSALA: MONTH-YEAR-COUNTRY OF ORIGIN ARMED CONFLICTS ATTACKS ******
***************************************************************************

* LOAD ORIGINAL DATASET
use "$uppsala/armedconflict_uppsala.dta", clear

* Fix date variables

cap drop month day

*gen year = substr(date_start,1,4)  // variable year is already present
gen month = substr(date_start,6,2)  
gen day = substr(date_start,9,2)

destring year, replace
destring month, replace
destring day, replace

* Generate day-month-year and month-year variables    
gen startdate = mdy(month,day,year) 
format startdate %td          

gen startmy = ym(year,month)
format startmy %tm


* Keep only necessary variables
keep id year month day startmy country_id country type_of_violence  conflict_new_id 
rename type_of_violence violence_type

gen startdate = mdy(month,day,year)
gen uppsala = 1
gen armedconflict = 1

lab def violence_type 1"State violence" 2"Non-state violence" 3"One-side violence"
lab val violence_type violence_type

g state_viol=0
replace state_viol=1 if violence_type==1
g nonstate_viol=0
replace nonstate_viol=1 if violence_type==2
g onsided_viol=0
replace onsided_viol=1 if violence_type==3
g statenonstate=0
replace statenonstate=1 if violence_type==1
replace statenonstate=1 if violence_type==2
	
* Label vars
lab var country "Country name"
lab var country_id "Country ID"
lab var startmy "Month-year of the event"
lab var day "Day of the event"
lab var month "Month of the event"
lab var year "Year of the event"
lab var uppsala "Indicator for any event in Uppsala Armed conflict data"
lab var armedconflict "Total armed conflict events in a day-country"
lab var violence_type "Type of violence"

			
* Merge with ieb-country codes
			preserve 
			use "$uppsala/help/countrycodes_ieb_armedconflict.dta", clear
				
				bys country_id: gen unique = 1 if _n == 1
				keep if unique == 1
				drop unique
			
			tempfile countrycodes_ieb_terror
			
			save `countrycodes_ieb_terror'

			restore
			
merge m:1 country_id using `countrycodes_ieb_terror', keep(3) 
		
		
* Label variables 

lab var startmy "Month-year of the event"
lab var day "Day of the event"
lab var month "Month of the event"
lab var year "Year of the event"
lab var startdate "Complete date of event start"

lab var country "Country name"
lab var country_id "Country code" 
			
			
* Collapse at the monthly-country of origin level
preserve 

foreach v of var * {
	local l`v' : variable label `v'
	   if `"`l`v''"' == "" {
		local l`v' "`v'"
	}
 }

collapse (sum) nkill* armedconflict *_viol statenonstate uppsala  (firstnm) violence_type  country country_id month year, by(ieb_nat_id startmy) 

foreach v of var * {
	label var `v' `"`l`v''"'
 }

save "$uppsala/intermediate/armedconflict_monthly_ieb.dta", replace	 
restore 



*************************************************************
** GTD: MONTH-YEAR-COUNTRY OF ORIGIN TERRORIST ATTACKS ******
*************************************************************

* Load original GTD database
import delimited "$globalterror/orig\globalterror_1970_2017.csv", clear  

	* Some people country of origin is an area that no longer exists (e.g., USSR, Yugoslavia)
	append using "$globalterror/help\terror_dd_formeryugos.dta", generate(yugo_countries) // use former yugoslavia to have backward information on countries
	append using "$globalterror/help\terror_dd_countriesyugo.dta", generate(countries_yugo) // use countries to compute forward events of yugoslavia
	append using "$globalterror/help\terror_dd_easternbloc.dta", generate(countries_eastern) // use countries to compute forward and backward events for the former eastern bloc (from post-soviet countries)


* Date of event 			
	* Complete date of the event
	gen startdate = mdy(imonth,iday,iyear)	
	* Month-year of the event
	gen startmy = ym(iyear,imonth) 
	format startmy %tm 
			
	rename imonth month
	rename iyear year
	rename iday day 
			
		
* Indicator for each event	
gen gtd = 1
clonevar terror = gtd
clonevar terror_suc = success
			
	
*  Fix country code variable 	
rename country_txt country_name 
rename country country_code 
	
replace country_name = "Eswatini" if inlist(country_name,"Swaziland") 
replace country_name = "Russia" if inlist(country_name,"Soviet Union") 
replace country_name = "Côte d'Ivoire" if inlist(country_name,"Ivory Coast") 
replace country_name = "Italy" if inlist(country_name,"Vatican City")
	 

	
* Merge with IEB country codes
	preserve 
	use "$globalterror/help/countrycodes_ieb_terror.dta", clear
	bys country_code: gen unique = 1 if _n == 1
	keep if unique == 1
	drop unique

	tempfile countrycodes_ieb_terror
	save `countrycodes_ieb_terror'
	restore


	merge m:1 country_code using `countrycodes_ieb_terror', keep(3) 


* Label variables 
lab var startmy "Month-year of the event"
lab var day "Day of the event"
lab var month "Month of the event"
lab var year "Year of the event"
lab var startdate "Complete date of event start"
lab var gtd "Indicator for any event in the gtd data"
lab var terror "Indicator for any terror event in the gtd data"
*lab var nkill_terror "Number of killed"
*lab var nwound_terror "Number of wounded"
lab var country_name "Country name"
lab var country_code "Country code in gtd" 
	
	
* Collapse data at the month-year level
preserve
foreach v of var * {
		local l`v' : variable label `v'
		   if `"`l`v''"' == "" {
			local l`v' "`v'"
		}
}

collapse (sum) terror_suc /*terror_*aff terror_*k*/ terror  success /*nkill* nwound* naffect**/ (mean) gtd   (firstnm) country_name country_code year month, by(ieb_nat_id startmy) 
	
	
foreach v of var * {
	label var `v' `"`l`v''"'
 }

	 
save "$globalterror/intermediate/allevents_monthly_ieb_final.dta", replace	 
restore	 

* Collapse daily level	
preserve		
foreach v of var * {
	local l`v' : variable label `v'
	   if `"`l`v''"' == "" {
		local l`v' "`v'"
	}
 }

collapse (sum) terror_suc /*terror_*aff terror_*k*/ terror  success /*nkill* nwound* naffect**/ (mean) gtd   (firstnm) country_name country_code year month day startmy, by(ieb_nat_id startdate) 
drop if startdate==.

foreach v of var * {
	label var `v' `"`l`v''"'
 }

 
save "$globalterror/intermediate/allevents_daily_ieb_final.dta", replace	 
restore	 
